package cn.itcast.waterboss.dao;

import java.sql.SQLException;

import cn.itcast.waterboss.entity.Owners;

/**
 * 业主信息数据访问类
 * 
 * @author smile
 *
 */
public class OwnersDao {

	public static void add(Owners owners) {

		java.sql.Connection conn = null;
		java.sql.PreparedStatement stmt = null;

		try {
			conn = BaseDao.getConnect();
			stmt = conn.prepareStatement("insert into t_owners values(?,?,?,?,?,?,?)");
			stmt.setLong(1, owners.getId());
			stmt.setString(2, owners.getName());
			stmt.setLong(3, owners.getAddress());
			stmt.setString(4, owners.getHouseNumber());
			stmt.setString(5, owners.getWaterMeter());
			stmt.setDate(6, new java.sql.Date(owners.getAddDate().getTime()));
			stmt.setLong(7, owners.getOwnerTypeId());
			stmt.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.close(null, stmt, conn);
		}

	}

	public static void update(Owners owners) {

		java.sql.Connection conn = null;
		java.sql.PreparedStatement stmt = null;

		try {
			conn = BaseDao.getConnect();
			stmt = conn.prepareStatement("update t_owners set name=?,addressid=?,housenumber=?,"
					+ "watermeter=?,adddate=?, ownertypeid=? where id=?");
			stmt.setLong(7, owners.getId());
			stmt.setString(1, owners.getName());
			stmt.setLong(2, owners.getAddress());
			stmt.setString(3, owners.getHouseNumber());
			stmt.setString(4, owners.getWaterMeter());
			stmt.setDate(5, new java.sql.Date(owners.getAddDate().getTime()));
			stmt.setLong(6, owners.getOwnerTypeId());
			stmt.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.close(null, stmt, conn);
		}

	}

	public static void delete(Long id) {

		java.sql.Connection conn = null;
		java.sql.PreparedStatement stmt = null;

		try {
			conn = BaseDao.getConnect();
			stmt = conn.prepareStatement("delete from t_owners where id = ?");
			stmt.setLong(1, id);
			stmt.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.close(null, stmt, conn);
		}

	}

}
